﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;


namespace PDAMain.Forms.CustomerWork
{
    public partial class CustomerSelect : Form
    {
        public CustomerSelect()
        {
            InitializeComponent();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            //獲取是否待處理
            string strStatus = string.Empty;
            if (Pending.Checked == true)
                strStatus = "1";
            else if (Processed.Checked == true)
                strStatus = "9";

            //獲取查詢時間段
            string strTime = Convert.ToDateTime("1900/01/01 00:00:00").ToString(); //初始化時間為1900年
            DateTime dt = DateTime.Now;
            if (oneDay.Checked == true)
            {
                strTime = dt.AddDays(-1).ToString("yyyy/MM/dd hh:mm:ss");
            }
            else if (ThreeDays.Checked == true)
            {
                strTime = dt.AddDays(-3).ToString("yyyy/MM/dd hh:mm:ss");
            }
            else if (oneWeek.Checked == true)
            {
                strTime = dt.AddDays(-7).ToString("yyyy/MM/dd hh:mm:ss");
            }
          
            //獲取數據
            DataTable dt1 = new DataTable();
            DataSet ds = null;
            try
            {
                ds = DataHelper.CustomerWorkDao.select(strStatus, strTime);
            }
            catch
            {
                ds = DataHelper.CustomerWorkDao.CustomerSelect(strStatus);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        if (Convert.ToDateTime(ds.Tables[0].Rows[index]["UPDATEDT"]) < Convert.ToDateTime(strTime))
                        {
                            ds.Tables[0].Rows.RemoveAt(index);
                        }
                    }
                }
            }
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                QueryGrid.DataSource = null;
                QueryGrid.TableStyles.Clear();
                QueryGrid.Refresh();
                MessageBox.Show("无数据");
                return;
            }

            dt1 = ds.Tables[0];
            for (int k = 0; k < dt1.Rows.Count;k++ ) 
            {

                if (dt1.Rows[k]["workStatus"].ToString() == "1") 
                {
                    dt1.Rows[k]["workStatus"] = "处理中";
                }
                else if (dt1.Rows[k]["workStatus"].ToString() == "2") 
                {
                    dt1.Rows[k]["workStatus"] = "转单";
                }
                else if (dt1.Rows[k]["workStatus"].ToString() == "3")
                {
                    dt1.Rows[k]["workStatus"] = "报修";
                }
                else if (dt1.Rows[k]["workStatus"].ToString() =="9")
                {
                    dt1.Rows[k]["workStatus"] = "已处理";
                }
                else
                {
                }
            }
            //綁定數據
            QueryGrid.DataSource = dt1;
            DataGridTableStyle dts = new DataGridTableStyle();
            dts.MappingName = dt1.TableName;

            DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
            col1.HeaderText = "活动编号";
            col1.MappingName = "activityUID";
            col1.Width = 83;
            dts.GridColumnStyles.Add(col1);

            DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
            col2.HeaderText = "地址";
            col2.MappingName = "houseLocation";
            col2.Width = 110;
            dts.GridColumnStyles.Add(col2);

            DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
            col3.HeaderText = "优先级";
            col3.MappingName = "workPriority";
            col3.Width = 60;
            dts.GridColumnStyles.Add(col3);

            DataGridTextBoxColumn col4 = new DataGridTextBoxColumn();
            col4.HeaderText = "状态";
            col4.MappingName = "workStatus";
            col4.Width = 60;
            dts.GridColumnStyles.Add(col4);

            QueryGrid.TableStyles.Clear();
            QueryGrid.TableStyles.Add(dts);
            QueryGrid.Refresh();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //返回至客服工單列表
            CustomerList list = new CustomerList();
            list.Show();
            this.Close();
        }

        private void Pending_Click(object sender, EventArgs e)
        {
            Processed.Checked = false;
        }

        private void Processed_Click(object sender, EventArgs e)
        {
            Pending.Checked = false;
        }

        private void oneDay_CheckStateChanged(object sender, EventArgs e)
        {
            if (oneDay.Checked == true)
            {
                ThreeDays.Checked = false;
                oneWeek.Checked = false;
            }
        }

        private void ThreeDays_CheckStateChanged(object sender, EventArgs e)
        {
            if (ThreeDays.Checked == true)
            {
                oneDay.Checked = false;
                oneWeek.Checked = false;
            }
        }

        private void oneWeek_CheckStateChanged(object sender, EventArgs e)
        {
            if(oneWeek.Checked == true)
            {
                oneDay.Checked = false;
                ThreeDays.Checked = false;}
        }


   
    }
}